#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct LNode {
    ElemType data;
    LNode *next;
} LNode, *LinkList;

//头插法
void List_Head_Insert(LinkList &L) {
    LinkList p;//新申请的结点
    L = (LinkList) malloc(sizeof(LNode));
    L->next = NULL;
    ElemType num;
    scanf("%d", &num);
    while (num != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = num;
        p->next = L->next;
        L->next = p;
        scanf("%d ", &num);
    }
}

//尾插法
void List_Tail_Insert(LinkList &L) {
    ElemType num;
    LinkList p, q;
    L = (LinkList) malloc(sizeof(LNode));
    q = L;
    scanf("%d", &num);
    while (num != 9999) {
        p = (LinkList) malloc(sizeof(LNode));
        p->data = num;
        q->next = p;
        q = p;
        scanf("%d", &num);
    }
    p->next = NULL;
}

//链表输出
void Print_list(LinkList L) {
    L = L->next;
    while (L) {
        printf("%d", L->data);
        L = L->next;
        if (L != NULL) {
            printf(" ");
        }
    }
    printf("\n");
}

int main() {
    LinkList arr, list;
    List_Head_Insert(list);
    List_Tail_Insert(arr);
    Print_list(list);
    Print_list(arr);
    return 0;
}
